import { _decorator, Component, Node, Vec3, CCFloat } from "cc";

const { ccclass, property } = _decorator;

@ccclass("ParabolicPath")
export class ParabolicPath extends Component {
    @property({ type: CCFloat, tooltip: "抛物线最大高度" })
    maxHeight: number = 10;

    @property({ type: CCFloat, tooltip: "飞行速度" })
    speed: number = 5;

    private startPos: Vec3 = new Vec3(); // 起始坐标
    private endPos: Vec3 = new Vec3();   // 终点坐标
    private totalTime: number = 0;       // 飞行总时间
    private currentTime: number = 0;    // 当前飞行时间

    onLoad() {
  
    }
    public init(a:Vec3,b:Vec3){
      // 设置起始坐标和终点坐标
      this.startPos.set(a.x, a.y, a.z);
      this.endPos.set(b.x, b.y, b.z);

      // 计算飞行总时间
      const distance = Vec3.distance(this.startPos, this.endPos);
      this.totalTime = distance / this.speed;
    }

    update(dt: number) {
        if (this.currentTime < this.totalTime) {
            this.currentTime += dt;

            if (this.currentTime > this.totalTime) {
                this.currentTime = this.totalTime;
            }

            const percent = this.currentTime / this.totalTime;

            // 计算抛物线高度
            const height = this.maxHeight * Math.sin(percent * Math.PI);

            // 计算当前位置
            const currentPos = new Vec3(
                this.startPos.x + percent * (this.endPos.x - this.startPos.x),
                this.startPos.y + height,
                this.startPos.z + percent * (this.endPos.z - this.startPos.z)
            );

            // 将对象移动到当前位置
            this.node.setWorldPosition(currentPos);
        }
    }
}
